package kevin.socketdemo;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class SendMeaageImpl implements  ISendMessage {

    private  SocketClientInstance socketClientInstance;

    private Object data;

    public SendMeaageImpl() {
    }

    public SendMeaageImpl(SocketClientInstance socketClientInstance, Object data) {
        this.socketClientInstance = socketClientInstance;
        this.data = data;
    }

    @Override
    public Object hello() {



        return sendMessage();
    }

    @Override
    public Object say() {

         return sendMessage();
    }


    /**
     * 发送消息
     * @return
     */
    public  Object sendMessage()
    {
        try {
            Socket socket = socketClientInstance.getSocketInstance();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(data);
            objectOutputStream.flush();




            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            Object data= inputStream.readObject();
            objectOutputStream.close();
            inputStream.close();

           return  data;

        }catch (Exception e)
        {
            e.printStackTrace();
        }
            return null;


    }

}
